package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetMealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/setmeal")
@Api(tags = "套餐相关接口")
@Slf4j
public class SetMealController {

    @Autowired
    private SetMealService setMealService;


    /**
     * 新增套餐
     * @param setmealDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增套餐")
    @CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.categoryId")

    public Result save(@RequestBody SetmealDTO setmealDTO) {
        log.info("新增套餐,setmealDTO={}",setmealDTO);
        setMealService.saveWithDish(setmealDTO);
        return Result.success();
    }

    /**
     * 分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页查询")
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO){
        log.info("分页查询");
        PageResult pageResult = setMealService.pageQuery(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 根据 id 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("根据 id 批量删除")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result deleteByIds(@RequestParam  List<Long> ids) {
        log.info("根据 ids={}批量删除 ",ids);
        setMealService.deleteBatch(ids);
        return  Result.success();
    }


    /**
     * 根据id查询套餐信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据 id 查询 套餐信息 回写 ")
    public Result<SetmealVO> getById(@PathVariable  Long id){
        log.info("根据 套餐 id:{} 查询套餐信息 及其 关联的菜品信息 ");
        SetmealVO setmealVO =  setMealService.getByIdWithDish(id);
        return Result.success(setmealVO);
    }


    /**
     * 更新 套餐信息
     * @param setmealDTO
     * @return
     */
    @PutMapping
    @ApiOperation("更新套餐信息 ")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result update(@RequestBody  SetmealDTO setmealDTO) {
        log.info("更新套餐信息 {}",setmealDTO);
        setMealService.update(setmealDTO);
        return Result.success();
    }

    /**
     * 起售或者停售
     * @param status
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("起售或者停售")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result startOrStop(@PathVariable Integer status,Long id) {
        log.info("起售或者停售");
        setMealService.startOrStop(status,id);
        return Result.success();

    }

}
